package cn.nfwx.tv.widget;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.lcodecore.tkrefreshlayout.IBottomView;

import cn.nfwx.tv.R;

/**
 * Created by zhouzechao on 2017/12/15.
 */

public class WxLoadmoreView extends FrameLayout implements IBottomView {
    private ImageView refreshArrow;
    private TextView refreshTextView;
    private ImageView loadingView;
    private Animation operatingAnim;

    public WxLoadmoreView(Context context) {
        this(context, null);
    }

    public WxLoadmoreView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WxLoadmoreView(Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View rootView = View.inflate(getContext(), R.layout.view_loadmorebottom, null);
        refreshArrow = (ImageView) rootView.findViewById(R.id.iv_arrow);
        refreshTextView = (TextView) rootView.findViewById(R.id.tv);
        loadingView = (ImageView) rootView.findViewById(R.id.iv_loading);
        refreshTextView.setVisibility(GONE);
        loadingView.setImageResource(R.mipmap.mine_round);
        if (operatingAnim != null) {
            loadingView.setAnimation(operatingAnim);
            loadingView.startAnimation(operatingAnim);
        } else {
            operatingAnim = AnimationUtils.loadAnimation(loadingView.getContext(), R.anim.rotate_anim);
            LinearInterpolator lin = new LinearInterpolator();
            operatingAnim.setInterpolator(lin);
            loadingView.setAnimation(operatingAnim);
            loadingView.startAnimation(operatingAnim);
        }
        addView(rootView);
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public void onPullingUp(float fraction, float maxBottomHeight, float bottomHeight) {
        //refreshArrow.setRotation(fraction * bottomHeight / maxBottomHeight * 180);
    }

    @Override
    public void startAnim(float maxBottomHeight, float bottomHeight) {
        refreshArrow.setVisibility(GONE);
        loadingView.setVisibility(VISIBLE);
    }

    @Override
    public void onPullReleasing(float fraction, float maxBottomHeight, float bottomHeight) {
    }

    @Override
    public void onFinish() {
        loadingView.clearAnimation();
    }

    @Override
    public void reset() {
        refreshArrow.setVisibility(GONE);
        loadingView.setVisibility(VISIBLE);
        if (operatingAnim != null) {
            loadingView.setAnimation(operatingAnim);
            loadingView.startAnimation(operatingAnim);
        } else {
            operatingAnim = AnimationUtils.loadAnimation(loadingView.getContext(), R.anim.rotate_anim);
            LinearInterpolator lin = new LinearInterpolator();
            operatingAnim.setInterpolator(lin);
            loadingView.setAnimation(operatingAnim);
            loadingView.startAnimation(operatingAnim);
        }
    }
}
